//
//  RefreshFooter.m
//  YunDongGym
//
//  Created by lqq on 2018/7/25.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "RefreshFooter.h"
#import "WCGraintCircleLayer.h"
#import "UIColor+Extension.h"

@interface RefreshFooter ()
@property (nonatomic, strong) WCGraintCircleLayer *circleLayer;
@property (nonatomic, strong) UILabel *loadingLabel;
@property (nonatomic, strong) UILabel *noMoreLabel;
@end
@implementation RefreshFooter

#pragma mark - 重写方法
#pragma mark 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    // 设置控件的高度
    self.mj_h = 50;
    UILabel *noMoreLabel = [[UILabel alloc] init];
    noMoreLabel.font = RegularFont(14);
    noMoreLabel.textColor = [UIColor colorWithHexString:@"333333"];
    noMoreLabel.textAlignment = NSTextAlignmentCenter;
    noMoreLabel.text = @"- 已到底部 -";
    [self addSubview:noMoreLabel];
    self.noMoreLabel = noMoreLabel;
    
    _circleLayer = [[WCGraintCircleLayer alloc] initGraintCircleWithBounds:CGRectMake(0, 0, 14, 14) Position:CGPointMake(0, 0) FromColor:[UIColor colorWithHexString:@"#3AA7FF" alpha:0] ToColor:[UIColor colorWithHexString:@"#3AA7FF"] LineWidth:2.0];
    [self.layer addSublayer:_circleLayer];
    
    
    UILabel *loadingLabel = [[UILabel alloc] init];
    loadingLabel.font = RegularFont(14);
    loadingLabel.textColor = [UIColor colorWithHexString:@"333333"];
    loadingLabel.textAlignment = NSTextAlignmentCenter;
    loadingLabel.text = @"正在努力加载";
    [loadingLabel sizeToFit];
    [self addSubview:loadingLabel];
    self.loadingLabel = loadingLabel;
    
    
}

- (void)startAnimation
{
    [self removeAnimation];
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = CGFLOAT_MAX;
    
    [_circleLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
- (void)stopAnimation
{
    [self removeAnimation];
}

- (void)removeAnimation
{
    [_circleLayer removeAllAnimations];
}
#pragma mark 在这里设置子控件的位置和尺寸
- (void)placeSubviews
{
    [super placeSubviews];
    
    self.noMoreLabel.frame = self.bounds;
    self.loadingLabel.center = CGPointMake(self.mj_size.width / 2 -12, self.mj_size.height / 2);
    self.circleLayer.position = CGPointMake(self.loadingLabel.right + 13, self.mj_h*0.5);
}

#pragma mark 监听scrollView的contentOffset改变
- (void)scrollViewContentOffsetDidChange:(NSDictionary *)change
{
    [super scrollViewContentOffsetDidChange:change];
    
}

#pragma mark 监听scrollView的contentSize改变
- (void)scrollViewContentSizeDidChange:(NSDictionary *)change
{
    [super scrollViewContentSizeDidChange:change];
    
}

#pragma mark 监听scrollView的拖拽状态改变
- (void)scrollViewPanStateDidChange:(NSDictionary *)change
{
    [super scrollViewPanStateDidChange:change];
    
}

#pragma mark 监听控件的刷新状态
- (void)setState:(MJRefreshState)state
{
    MJRefreshCheckState;
    NSLog(@"%zd",state);
    switch (state) {
        case MJRefreshStateIdle:
        {
            [self stopAnimation];
            self.circleLayer.hidden = YES;
            self.noMoreLabel.hidden = YES;
            self.loadingLabel.hidden = YES;
            break;
        }
        case MJRefreshStatePulling:
        {
            [self startAnimation];
            self.circleLayer.hidden = NO;
            self.loadingLabel.hidden = NO;
            self.noMoreLabel.hidden = YES;
            break;
        }
        case MJRefreshStateRefreshing:
        {
            [self startAnimation];
            self.circleLayer.hidden = NO;
            self.loadingLabel.hidden = NO;
            self.noMoreLabel.hidden = YES;
        }
            break;
        case MJRefreshStateNoMoreData:
        {
            [self stopAnimation];
            self.circleLayer.hidden = YES;
            self.loadingLabel.hidden = YES;
            self.noMoreLabel.hidden = NO;
        }
        default:
            break;
    }
}

#pragma mark 监听拖拽比例（控件被拖出来的比例）
- (void)setPullingPercent:(CGFloat)pullingPercent
{
    [super setPullingPercent:pullingPercent];
    
    // 1.0 0.5 0.0
    // 0.5 0.0 0.5
    
}

@end
